<?php
// +--------------------------------------------------------------------------
// | ISDCE [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2017 载攸道 版权所有 <TEL:086-010-57209502>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * [ 后台模块 ]
 */
namespace Admin\Controller;
use Common\Controller\AdminController;
use Think\Db;

/**
 * 数据管理控制器
 */
class DataController extends AdminController{
	
	/**
	 * 数据管理
	 */
    public function Manage(){
		
		action_log('admin_data_manage');//行为记录
		
		//页面配置
		$page = array(
			
			'title'=>'数据管理',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		
		);
		
		$this->assign('page',$page);
		
		//获得数据表信息
		$Db    = Db::getInstance();
		$list  = $Db->query('SHOW TABLE STATUS');
		$list  = array_map('array_change_key_case', $list);
				
		$this->assign('list', $list);
		
		
        $this->display();
	}
	
	/**
	 * 备份数据库。
	 */
	public function Backup() {
		
		action_log('admin_data_backup');//行为记录
		
		if(IS_AJAX){
			
			$DataDir = "../Backup/database/mysql/";
			
			mkdirs($DataDir);
		   
			$config = array(
				'host' => C('DB_HOST'),
				'port' => C('DB_PORT'),
				'userName' => C('DB_USER'),
				'userPassword' => C('DB_PWD'),
				'dbprefix' => C('DB_PREFIX'),
				'charset' => 'UTF8',
				'path' => $DataDir,
				'isCompress' => 0, //是否开启gzip压缩
				'isDownload' => 0
			);
			
			$mr = new \Org\Util\MySQLReback($config);
			
			$mr->setDBName(C('DB_NAME'));
			
			//自定义文件名称
			$time = time();
			
			$backup_file_name = $time . '_' . date('YmdHis',$time) . '_database.sql';
			
			$result = $mr->backup($backup_file_name);
			
			$info = $result?get_errcode(0):get_errcode(-1);
		  
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

			
        $this->ajaxReturn($info);
		
			
    }
	
	/**
	 * 优化所有表
	 */
    public function Optimize(){
      
		action_log('admin_data_optimize');//行为记录
		
		if(IS_AJAX){

			//获得所有数据表
			$Db    = Db::getInstance();
			$tlist  = $Db->query('SHOW TABLE STATUS');
			$tlist  = array_map('array_change_key_case', $tlist);
			
			foreach($tlist as $i=>$k){
				
				$tables[$i] = $tlist[$i]['name'];
				
			}
				
			if(is_array($tables)){
				$tables = implode('`,`', $tables);
				$result = $Db->query("OPTIMIZE TABLE `{$tables}`");

				//返回信息
				$info = $result?get_errcode(0):get_errcode(-1);
					
			}
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

			
       $this->ajaxReturn($info);
    }
	
	/**
	 * 下载备份的MySQL数据库
	 */
	public function Download(){
		
		action_log('admin_data_download');//行为记录
		
		$DataDir = "../Backup/database/mysql/";
		
		$file = I('param.file');
		 //获得下载的文件
		getFileDownload($file,$DataDir);
		
		

    }
	
	/**
	 * 修复所有表
	 */
    public function Repair(){
		
		action_log('admin_data_repair');//行为记录
		
		if(IS_AJAX){

			//获得所有数据表
			$Db    = Db::getInstance();
			$tlist  = $Db->query('SHOW TABLE STATUS');
			$tlist  = array_map('array_change_key_case', $tlist);
			
			foreach($tlist as $i=>$k){
				
				$tables[$i] = $tlist[$i]['name'];
				
			}
				
			if(is_array($tables)){
				$tables = implode('`,`', $tables);
				$result = $Db->query("REPAIR TABLE `{$tables}`");

				//返回信息
				$info = $result?get_errcode(0):get_errcode(-1);
					
			}
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

			
       $this->ajaxReturn($info);
    }
	
	/**
	 * 查看数据库备份
	 */
    public function Log(){
		
		action_log('admin_data_log');//行为记录
		
		common_public()->assign_page('备份记录');
		
		$DataDir = "../Backup/database/mysql/";
		
		$backup = getDir($DataDir);
		
		arsort($backup);
		foreach($backup as $i=>$k){
			
			$list[$i]['name'] = $backup[$i];
			$list[$i]['size'] = format_bytes(filesize($DataDir.$backup[$i]));
			
			$file = explode("_",$backup[$i]);
			
			$list[$i]['time'] = $file[0];
			
			
			
		}
		
		$this->assign('list',$list);
		
		
		$this->display();
	}
	
	
	/**
	 * MySQL数据库还原 //TODO下一个版本完善
	 */
	public function Restore(){
		
		action_log('admin_data_restore');//行为记录
		
		if(IS_AJAX){
			
		
			//救赎主数据库(还原前备份实体数据库)
			$this->Redeemer();
			
			$DataDir = "../Backup/database/mysql/";
			
			$file = I('param.file','','');
			echo $file;
			$config = array(
				'host' => C('DB_HOST'),
				'port' => C('DB_PORT'),
				'userName' => C('DB_USER'),
				'userPassword' => C('DB_PWD'),
				'dbprefix' => C('DB_PREFIX'),
				'charset' => 'UTF8',
				'path' => $file['file_dir'],
				'isCompress' => 0, //是否开启gzip压缩 0为不开启
				'isDownload' => 0
			);
			
			$mr = new \Org\Util\MySQLReback($config);
			
			$mr->setDBName(C('DB_NAME'));
			
			$result = $mr->recover($DataDir.$file);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
					
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

			
		$this->ajaxReturn($info);
    }
	
	/**
	 * 救世主备份版 //TODO下一个版本完善
	 */
	public function Redeemer() {
		
		action_log('admin_data_redeemer');//行为记录
		
		$DataDir = "../Backup/database/mysql/";
		
		mkdirs($DataDir);
	   
		$config = array(
			'host' => C('DB_HOST'),
			'port' => C('DB_PORT'),
			'userName' => C('DB_USER'),
			'userPassword' => C('DB_PWD'),
			'dbprefix' => C('DB_PREFIX'),
			'charset' => 'UTF8',
			'path' => $DataDir,
			'isCompress' => 0, //是否开启gzip压缩
			'isDownload' => 0
		);
		
		$mr = new \Org\Util\MySQLReback($config);
		
		$mr->setDBName(C('DB_NAME'));
		
		//自定义文件名称
		$time = time();
		
		$backup_file_name = $time . '_' . date('YmdHis',$time) . '_database_redeemer.sql';
		
		$mr->backup($backup_file_name);
			
			
    }
	
	
}